<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class dynamicController extends Controller
{

    //执行查询 数据 为动态页数据
    public  function index()
    {

        $res = DB::table('dynamic')
                   ->join('user','user.id','=','dynamic.d_id')
                   ->join('dycont','dynamic.id','=','dycont.dy_id')
                   ->select('user.*','dycont.*','dynamic.*')->paginate(6);


            return view('admin.dynamic.dynamic', ['res' => $res]);
            die;

//        ->paginate(2)

//        $res = DB::select('select d.`d_posting`,d.`id`,d.`d_id`,d.`d_jur`,d.`d_type`,d.`d_equi`,d.`d_time`,d.`d_zf`,d.`d_pl`,d.`d_like`,u.`u_name`,c.`dy_cont`,i.`dm_img` from `dynamic` as d,`user` as u,`dycont` as c,`dyimg` as i  where u.`id` = d.`d_id` and d.`id` = i.`dm_id`  and d.`id` = c.`dy_id`');


    }

    //动态详情查询包含图片神马之类的
    public function details($id)
    {


        $res = DB::table('dynamic')
                        ->join('user','user.id','=','dynamic.d_id')
                        ->join('dycont','dynamic.id','=','dycont.dy_id')
                        ->join('dyimg','dynamic.id','=','dyimg.dm_id')
                        ->where('dycont.dy_id','=',$id)
                        ->select('dynamic.id','dynamic.d_id','dyimg.dm_img','dynamic.d_jur','dynamic.d_type','dynamic.d_equi','dynamic.d_time','dynamic.d_zf','dynamic.d_pl','dynamic.d_like','user.u_name','dycont.dy_cont','user.u_img')->get();

        //var_dump($res);die;
        if (count($res) != 0){
            //echo 11;die;
            return view('admin.dynamic.details',['res' => $res]);

        }else{

            //echo 22;die;
            $res = DB::table('dynamic')
                ->join('user','user.id','=','dynamic.d_id')
                ->join('dycont','dynamic.id','=','dycont.dy_id')
                ->where('dycont.dy_id','=',$id)
                ->select('dynamic.id','dynamic.d_id','dynamic.d_jur','dynamic.d_type','dynamic.d_equi','dynamic.d_time','dynamic.d_zf','dynamic.d_pl','dynamic.d_like','user.u_name','dycont.dy_cont','user.u_img')->get();

            return view('admin.dynamic.details',['res' => $res]);
            die;

        }

       //var_dump($res);die;

        //return view('admin.dynamic.details',['res' => $res]);
    }



    //删除动态
    public function del($id)
    {
        $id = $id;

        $res = DB::delete("delete from `dycont` where `dy_id` = ?",[$id]);



        if($res) {

            echo '1';

        }else{

            echo '2';
        }

    }


    //设置热门动态

    public function set($id,$posting)
    {

        $dis = $posting == 1 ?'2':'1';

        $res = DB::update("update `dynamic` set  `d_posting` = $dis where id = ?",[$id]);

        if ($res)
        {
            return back();

        }else{
            return back();
        }


    }

}
